﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Com.GUI;
using Air_condition.DO;
using Air_condition.BU;
using Com.DA;
using System.Reflection;


namespace Air_condition.GUI
{
    /// <summary>
    /// Interaction logic for TemplateManagement.xaml
    /// </summary>
    ///   

    public partial class TemplateManagement : Window
    {
        LibraryManagerInCom lbrManager = new LibraryManagerInCom();
        tb_heat_by_man manheat;
        Template temp = new Template();
        Glass glas=new Glass();
        Room room1 = new Room();
        private InternalLoad inter=new InternalLoad();
        private int icount = 0;
        private int icountInter = 0;
     
        Ceiling ceil = new Ceiling();
     
        Roof roof = new Roof();
        
        Wall wall = new Wall();

        Floor floor = new Floor();
       
        public TemplateManagement()
        {
            InitializeComponent();
            LoadLocation();
            LoadSpace();
            LoadTypeOfGlass();
            LoadGlassDirection();
            LoadPressure();
            LoadTime();
            LoadSolarRadiation();
            LoadCeilling();
            LoadCeillingLayer();
            LoadStructureRoof();
            LoadCharacteristicRoof();
            LoadRoofSurface();
            LibraryManagerInCom.loadHeatByMan();
            cbxGender.Items.Add("Male");
            cbxGender.Items.Add("Female");
            cbxGender.Items.Add("FeMale");
           
        }
        //---------------------------------------LOAD CÁC ĐỐI TƯỢNG--------------------------
        //-----------------------------------------------------------------------------------
        #region Loading
        private void LoadLocation()
        {
            cbxLocation.ItemsSource = null;
            cbxLocation.Items.Clear();
            LibraryManagerInCom.loadLocation();
            cbxLocation.ItemsSource = LibraryManagerInCom.LocationList;
            cbxLocation.DisplayMemberPath = "location";
            cbxLocation.SelectedIndex = 0;
        }

        private void LoadSpace()
        {
            cbxSpaceConditioning.ItemsSource = null;
            cbxSpaceConditioning.Items.Clear();
            LibraryManagerInCom.loadAirConditionPlace();
            cbxSpaceConditioning.ItemsSource = LibraryManagerInCom.AirConditionPlaceList;
            cbxSpaceConditioning.DisplayMemberPath = "description";
            cbxSpaceConditioning.SelectedIndex = 0;
        }
        //
        // Load loại kính
        private void LoadTypeOfGlass()
        {
            cbxTypeOfGlass.ItemsSource = null;
            cbxTypeOfGlass.Items.Clear();
            LibraryManagerInCom.loadTypeOfGlass();
            cbxTypeOfGlass.ItemsSource = LibraryManagerInCom.GlassCoeffientList;
            cbxTypeOfGlass.DisplayMemberPath = "glass_type";
            cbxTypeOfGlass.SelectedIndex = 0;
        }
        //  Load hướng của tường
        private void LoadGlassDirection()
        {
            cbxDirection.ItemsSource = null;
            cbxDirection.Items.Clear();
            LibraryManagerInCom.loadDirectionOfGlass();
            cbxDirection.ItemsSource = LibraryManagerInCom.GlassDirectionList;
            cbxDirection.DisplayMemberPath = "glass_direction";
            cbxDirection.SelectedIndex = 0;
        }
        //  Load số Pressure kg/m2 sàn
        private void LoadPressure()
        {
            cbxPressure.ItemsSource = null;
            cbxPressure.Items.Clear();
            LibraryManagerInCom.loadPressure();
            cbxPressure.ItemsSource = LibraryManagerInCom.ImmediateImpactList;
            cbxPressure.DisplayMemberPath = "weight_per_meter_square";
            cbxPressure.SelectedIndex = 0;
        }
        //  Load thời gian
        private void LoadTime()
        {
            cbxTime.ItemsSource = null;
            cbxTime.Items.Clear();
            LibraryManagerInCom.loadTime();
            cbxTime.ItemsSource = LibraryManagerInCom.ImmediateImpactList;
            cbxTime.DisplayMemberPath = "time";
            cbxTime.SelectedIndex = 0;
        }
        // Load lượng bức xạ mặt trời lớn nhất qua kính
        private void LoadSolarRadiation()
        {
            cbxHeatBySolarRadiation.ItemsSource = null;
            cbxHeatBySolarRadiation.Items.Clear();
            LibraryManagerInCom.loadSolarByRadiation();
            cbxHeatBySolarRadiation.ItemsSource = LibraryManagerInCom.SolarRadiationList;
            cbxHeatBySolarRadiation.DisplayMemberPath = "radiation_amount";
            cbxHeatBySolarRadiation.SelectedIndex = 0;
        }
        // Load cấu tạo của trần và sàn
        private void LoadCeilling()
        {

            cbxStructureOfCeilling.ItemsSource = null;
            cbxStructureOfCeilling.Items.Clear();
            LibraryManagerInCom.loadCeilling();
            cbxStructureOfCeilling.ItemsSource = LibraryManagerInCom.CeilingList;
            cbxStructureOfCeilling.DisplayMemberPath = "description";
            cbxStructureOfCeilling.SelectedIndex = 0;
            //
            cbxStructureOfFloor.ItemsSource = null;
            cbxStructureOfFloor.Items.Clear();
            LibraryManagerInCom.loadCeilling();
            cbxStructureOfFloor.ItemsSource = LibraryManagerInCom.CeilingList;
            cbxStructureOfFloor.DisplayMemberPath = "description";
            cbxStructureOfFloor.SelectedIndex = 0;

        }

        // Load đặc điểm của trần và sàn
        private void LoadCeillingLayer()
        {
            cbxCharacteristicsOfCeilling.ItemsSource = null;
            cbxCharacteristicsOfCeilling.Items.Clear();
            LibraryManagerInCom.loadCeillingLayer();
            cbxCharacteristicsOfCeilling.ItemsSource = LibraryManagerInCom.CeilingLayerList;
            cbxCharacteristicsOfCeilling.DisplayMemberPath = "layer_type";
            cbxCharacteristicsOfCeilling.SelectedIndex = 0;
            //
            cbxCharacteristicsOfFloor.ItemsSource = null;
            cbxCharacteristicsOfFloor.Items.Clear();
            LibraryManagerInCom.loadCeillingLayer();
            cbxCharacteristicsOfFloor.ItemsSource = LibraryManagerInCom.CeilingLayerList;
            cbxCharacteristicsOfFloor.DisplayMemberPath = "layer_type";
            cbxCharacteristicsOfFloor.SelectedIndex = 0;
        }
        //  Load cấu tạo của mái
        private void LoadStructureRoof()
        {
            cbxStructureOfRoof.ItemsSource = null;
            cbxStructureOfRoof.Items.Clear();
            LibraryManagerInCom.loadStructureRoof();
            cbxStructureOfRoof.ItemsSource = LibraryManagerInCom.RoofCeilingStructureList;
            cbxStructureOfRoof.DisplayMemberPath = "roof_ceiling_structure";
            cbxStructureOfRoof.SelectedIndex = 0;
        }
        //  Load đặc điểm của mái
        private void LoadCharacteristicRoof()
        {
            cbxCharacteristicsOfRoof.ItemsSource = null;
            cbxCharacteristicsOfRoof.Items.Clear();
            LibraryManagerInCom.loadCharacteristicRoof();
            cbxCharacteristicsOfRoof.ItemsSource = LibraryManagerInCom.RoofCeilingCharacteristicList;
            cbxCharacteristicsOfRoof.DisplayMemberPath = "roof_ceiling_characteristic";
            cbxCharacteristicsOfRoof.SelectedIndex = 0;
        }
        //  Load loại mặt mái
        private void LoadRoofSurface()
        {
            cbxTypeOfSurface.ItemsSource = null;
            cbxTypeOfSurface.Items.Clear();
            LibraryManagerInCom.loadRoofSurface();
            cbxTypeOfSurface.ItemsSource = LibraryManagerInCom.RoofSurfaceList;
            cbxTypeOfSurface.DisplayMemberPath = "surface_roof";
            cbxTypeOfSurface.SelectedIndex = 0;
        }
        #endregion
        //----------------------------------------ROOM TAB------------------------------------
        //------------------------------------------------------------------------------------
        #region Room
        //  luu room  
        private void addRoom()
        {
            bool checking = true;
            Room room = new Room();
            if (cmbDes_room.Text != "")
            {
                foreach (Room ro1 in temp.oroom)
                {
                    if (cmbDes_room.Text == ro1.sName)
                    {
                        MessageBox.Show("Creating new name, please!");
                        checking = false;
                    }
                }
                if (checking)
                {
                    room.sName = cmbDes_room.Text;
                }
            }
            else
            {
                room.sName = "Room " +"-"+ (temp.oroom.Count + 1);
            }
            if (checking)
            {

                temp.oroom.Add(room);
 
            }
        }
        //
        private void ApplyRoom(Room room)
        {
            try
            {
                room.sName = cmbDes_room.Text.ToString();
                room.iName = cmbDes_room.SelectedIndex;
                room.dbOutsideTemperature = Convert.ToDouble(txtOutsideTemperature.Text);
                room.dbInsideTemperature = Convert.ToDouble(txtInsideTemperature.Text);
                room.dbOutsideAirContainingLevel = Convert.ToDouble(txtOutsideAirContainingLevel.Text);
                room.dbInsideAirContainingLevel = Convert.ToDouble(txtInsideAirContainingLevel.Text);
                room.dbHeigh = Convert.ToDouble(txtHeighRoom.Text);
                room.dbWidth = Convert.ToDouble(txtWidthRoom.Text);
                room.dbLength = Convert.ToDouble(txtLengthRoom.Text);
                room.sOutsideActiveAir = Convert.ToDouble(txtOutsideActiveAir.Text);
                room.iLocation = cbxLocation.SelectedIndex;
                room.iSpaceConditioning = cbxSpaceConditioning.SelectedIndex;
            }
            catch (Exception es)
            {
                MessageBox.Show("###");
            }
         
        }
        //  Xoa room
        private void DeleteRoom()
        {
            if (temp.oroom.Count == 0)
            {
                btnDelete.IsEnabled = true;
                icount = 0;
            }
            else
            {
                temp.oroom.Remove((Room)cmbDes_room.SelectedItem);
                Loadspacing();
            }
        }
        // Tao  room moi / sau khi click new cần ẩn các button close/delete/new.

        private void Loadspacing()
        {
            txtInsideTemperature.Text = "";
            txtOutsideAirContainingLevel.Text = "";
            txtInsideAirContainingLevel.Text = "";
            txtHeighRoom.Text = "";
            txtWidthRoom.Text = "";
            txtLengthRoom.Text = "";
            txtOutsideActiveAir.Text = "";
        }
        //  Coppy room tu room hien tai 
        private void Coppyroom()
        {
            icount++;
            if (cmbDes_room.SelectedItem == null)
            {
                MessageBox.Show("Input paramenter, please");
            }
            else
            {

                Room r1 = Activator.CreateInstance<Room>();
                //  sao chep cac thuoc tinh
                foreach (PropertyInfo i in cmbDes_room.SelectedItem.GetType().GetProperties())
                {
                    object value = cmbDes_room.SelectedItem.GetType().GetProperty(i.Name).GetValue(cmbDes_room.SelectedItem, null);
                    i.SetValue(r1, value, null);
                }
                r1.sName = "Room"+"-"+icount;
                temp.oroom.Add(r1);
                LoadtabRoom();
            }
        }

        //  tab room
        private void btnApply_Click(object sender, RoutedEventArgs e)
        {


            if (temp.oroom.Count != 0 && (Room)cmbDes_room.SelectedItem!=null)
            {
                ApplyRoom(room1);
                Reloadroom();
            }
            else {
                MessageBox.Show("Input Paramener, please!");
            }
          
        }
        //  Lam moi lai danh sach cau o combobox
        private void Reloadroom()
        {
            cmbDes_room.ItemsSource = null;
            cmbDes_room.Items.Clear();
            cmbDes_room.ItemsSource = temp.oroom;
            cmbDes_room.DisplayMemberPath = "sName";
            cmbDes_room.SelectedIndex = 0;
        }
        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
       
            Coppyroom();
            Reloadroom();
        }
        // su kien tao phong moi 
        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            icount++;
            btnDelete.IsEnabled = true;
            btnApply.IsEnabled = true;
            btnCopy.IsEnabled = true;
            //try
            //{
            //    if (cmbDes_room.Text != "")
            //    {
            //        cmbDes_room.Text = "Room" +"-"+ icount;
            //    }
            //    if (cmbDes_room.Text == "" && temp.oroom.Count == 0)
            //    {
            //        icount = 0;
            //        cmbDes_room.Text = "Room" + "-" + icount;
            //        addRoom();

            //    }
            //    else
            //    {
            //      //  room.sName = cmbDes_room.Text.ToString();
                    addRoom();

                //}

                Reloadroom();
                LoadtabRoom();
            //}
            //catch (Exception ex)
            //{
            //    throw;
            //}


        }
        // su kien xoa phong 
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            DeleteRoom();
            Reloadroom();

        }

        //  chon lai mo ta 
        private void cmbDes_room_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //Room r1 = temp.oroom.SingleOrDefault(r=>r.iName);
            room1 = (Room)cmbDes_room.SelectedItem;
            string sfindname;
            sfindname = cmbDes_room.Text;
            if (room1 != null)
            {
                LoadtabRoom();
            }

        }
        //  Load thong so room kh nguoi dung chon lai 
        private void LoadtabRoom()
        {
            try
            {
                cbxLocation.SelectedIndex = room1.iLocation;
                txtOutsideTemperature.Text = room1.dbOutsideTemperature.ToString();
                txtInsideAirContainingLevel.Text = room1.dbInsideAirContainingLevel.ToString();
                txtInsideTemperature.Text = room1.dbInsideTemperature.ToString();
                txtLengthRoom.Text = room1.dbLength.ToString();
                txtWidthRoom.Text = room1.dbWidth.ToString();
                txtHeighRoom.Text = room1.dbHeigh.ToString();
                txtOutsideActiveAir.Text = room1.sOutsideActiveAir.ToString();
                cbxSpaceConditioning.SelectedIndex = room1.iSpaceConditioning;
            }catch(Exception ex){
                throw;
            }
          
        }
        private void validationInput(TextBox txt1)
        { 
            if(txt1.Text=="")
            {
                txt1.Text = "0";
            }
        }
        //
        #region RETURN 0 TO TEXTBOX IN ROOM
        //  Validation Room
        private void txtOutsideTemperature_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtOutsideTemperature);
        }

        private void txtOutsideAirContainingLevel_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtOutsideAirContainingLevel);
        }

        private void txtInsideAirContainingLevel_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtInsideAirContainingLevel);
        }

        private void txtWidthRoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthRoom);
        }

        private void txtHeighRoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtHeighRoom);
        }

        private void txtLengthRoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtLengthRoom);
        }

        private void txtOutsideActiveAir_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtOutsideActiveAir);
        }

        private void txtInsideTemperature_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtInsideTemperature);
        }
        //
        #endregion 
        private void cbxLocation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtOutsideTemperature.Text = (((tb_location)cbxLocation.SelectedItem).average_temperature + 0.125 * ((tb_location)cbxLocation.SelectedItem).max_temperature).ToString();
        }
       
        #endregion
        #region INTERNALLOAD TAB
        
        //  Delete Internalload
        private void DeleteInternal()
        {
            if (temp.ointernalload.Count == 0 && (InternalLoad)cbxdes_Internal.SelectedItem != null)
            {
                btnApply1.IsEnabled = false;
                btnCopy1.IsEnabled = false;
                btnDelete1.IsEnabled = false;
                icountInter = 0;
            }
            else
            {
                temp.ointernalload.Remove((InternalLoad)cbxdes_Internal.SelectedItem);
                
            }
        }
        //  luu  internal Load moi
        private void ApplyInternal(InternalLoad inter)
        {

            inter.sName = cbxdes_Internal.Text.ToString();
            if (cbxGender.Text == "Male")
            {
                inter.Gender = 1;
            }
            if(cbxGender.Text == "Female")
            {
                inter.Gender = 2;
            }
            else{
                inter.Gender = 3;
            }
            inter.PowerTools = Convert.ToDouble(txtEmittedHeatOfequipment.Text);
            inter.NumberPeople = Convert.ToInt32(txtNumberOfPeople.Text);
            inter.SensibleHeat = Convert.ToDouble(txtPeopleSensibleHeat.Text);
            inter.Gender = cbxGender.SelectedIndex;
            inter.LatentHeat = Convert.ToDouble(txtPeopleLatentHeat.Text);
            temp.ointernalload.Add(inter);
        }
        //  Tao moi internal load
        private void AddInternal()
        {
            InternalLoad inter = new InternalLoad();    
            //ApplyInternal(inter);
            bool check = true;

            if (cbxdes_Internal.Text != "")
            {
                foreach (InternalLoad ikk in temp.ointernalload)
                {
                    if (cbxdes_Internal.Text == ikk.sName)
                    {
                        MessageBox.Show("Creating new name, please!");
                        check = false;
                    }
                }
                if (check)
                {
                    inter.sName = cbxdes_Internal.Text;
                }
            }
            else
            {
                inter.sName = "Internal " + "-" + (temp.ointernalload.Count + 1);
            }
            if (check)
            {

                temp.ointernalload.Add(inter);

            }
            
           
        }
  
        //  Coppy internal Load
        private void CoppyInternal()
        {
            icountInter++;
            if (cbxdes_Internal.SelectedItem == null)
            {
                MessageBox.Show("Input paramenter, please");
            }
            else
            {
                InternalLoad coppyInternal = Activator.CreateInstance<InternalLoad>();
                foreach (PropertyInfo i in cbxdes_Internal.SelectedItem.GetType().GetProperties())
                {
                    object value = cbxdes_Internal.SelectedItem.GetType().GetProperty(i.Name).GetValue(cbxdes_Internal.SelectedItem, null);
                    i.SetValue(coppyInternal, value, null);
                }
                coppyInternal.sName = "Internal" + "-" + temp.ointernalload.Count + 1;
                temp.ointernalload.Add(coppyInternal);
            }
        }
        //  Load lai thong so combobox
        private void ReloadcomboxInter()
        {
            cbxdes_Internal.ItemsSource = null;
            cbxdes_Internal.Items.Clear();
            cbxdes_Internal.ItemsSource = temp.ointernalload;
            cbxdes_Internal.DisplayMemberPath = "sName";
            cbxdes_Internal.SelectedIndex = 0;
        }
 
        //  Load lai 1 doi tuong bat ki trong internal khi dc nguoi dung chon
        private void LoadformInternal(InternalLoad inter)
        {
            try
            {
                txtEmittedHeatOfequipment.Text = inter.PowerTools.ToString();
                txtNumberOfPeople.Text = inter.NumberPeople.ToString();
                txtPeopleSensibleHeat.Text = inter.SensibleHeat.ToString();
                txtPeopleLatentHeat.Text = inter.LatentHeat.ToString();
                cbxGender.SelectedItem = inter.Gender;
            }
            catch (Exception e)
            {
                MessageBox.Show("Create new object");
            }

        }

        //  Apply internal load 
        private void bntApply1_Click(object sender, RoutedEventArgs e)
        {
            ApplyInternal(inter);
            ReloadcomboxInter();
        }
        //  tao moi internalload
        private void btnNew1_Click(object sender, RoutedEventArgs e)
        {

            icountInter++;
            btnApply1.IsEnabled = true;
            btnCopy1.IsEnabled = true;
            btnDelete1.IsEnabled = true;
            //try
            //{
            //    if (cbxdes_Internal.Text != null)
            //    {
            //        cbxdes_Internal.Text = "Internal" + "-" + icountInter;
            //    }
            //    if (cbxdes_Internal.Text == "" && temp.ointernalload.Count == 0)
            //    {
            //        icountInter = 0;
            //        cbxdes_Internal.Text = "Internal" + "-" + icountInter;
            //        AddInternal();

            //    }
            //    else
            //    {
            //        cbxdes_Internal.Text = "Internal" + "-" + icountInter;
                   
                //}
                 AddInternal();
                ReloadcomboxInter();
                LoadformInternal(inter);
            //}
            //catch (Exception ex)
            //{
            //    throw;
            //}

        }
        //  sao chep internal load moi 
        private void btnCopy1_Click(object sender, RoutedEventArgs e)
        {
            CoppyInternal();
            ReloadcomboxInter();

        }
        //  xoa internal load hien tai 
        private void btnDelete1_Click(object sender, RoutedEventArgs e)
        {
            DeleteInternal();
            ReloadcomboxInter();
        }
        //
        private void cbxSpaceConditioning_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_air_conditioning_place conplace = (tb_air_conditioning_place)cbxSpaceConditioning.SelectedItem;
            txtNumberOfPeople.Text = conplace.people_density.ToString();
            //txtAmountAir.Text = conplace.air_amount_per_perople.ToString();
            double dbInsideTemperature = Convert.ToDouble(txtInsideTemperature.Text);
            manheat = LibraryManagerInCom.HeatByManList.SingleOrDefault(mh => mh.working_place_id == conplace.id && mh.working_place_temperature == dbInsideTemperature);
            if (manheat != null)
            {
                if (cbxGender.SelectedItem == "Male")
                {
                    txtPeopleLatentHeat.Text = manheat.latent_heat.ToString();
                    txtPeopleSensibleHeat.Text = manheat.sensible_heat.ToString();
                }
                else
                {
                    if (cbxGender.SelectedItem == "Female")
                    {
                        // Chưa đúng công thức
                        txtPeopleLatentHeat.Text = (manheat.latent_heat * 2).ToString();
                        txtPeopleSensibleHeat.Text = (manheat.sensible_heat * 2).ToString();
                    }
                    else
                    {
                        // Chưa đúng công thức
                        txtPeopleLatentHeat.Text = (manheat.latent_heat * 3).ToString();
                        txtPeopleSensibleHeat.Text = (manheat.sensible_heat * 3).ToString();
                    }
                }
                txtOutsideActiveAir.Text = (conplace.people_density * conplace.air_amount_per_perople).ToString();
            }
        }
        //  Thay doi khi chon gioi tinh 
        private void cbxGender_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxGender.SelectedItem == "Male")
            {
                // cbxGender.SelectedItem = true;
                if (manheat != null)
                {
                    txtPeopleLatentHeat.Text = manheat.latent_heat.ToString();
                    txtPeopleSensibleHeat.Text = manheat.sensible_heat.ToString();
                }
            }
            else
            {
                if (cbxGender.SelectedItem == "Female")
                {
                    //   cbxGender.SelectedItem = false;
                    if (manheat != null)
                    {
                        txtPeopleLatentHeat.Text = (manheat.latent_heat * 2).ToString();
                        txtPeopleSensibleHeat.Text = (manheat.sensible_heat * 2).ToString();
                    }
                }
                else
                {
                    if (manheat != null)
                    {
                        txtPeopleLatentHeat.Text = (manheat.latent_heat * 3).ToString();
                        txtPeopleSensibleHeat.Text = (manheat.sensible_heat * 3).ToString();
                    }
                }
            }
        }

        private void cbxdes_Internal_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            inter = (InternalLoad)cbxdes_Internal.SelectedItem;
            if (inter != null)
            {
                LoadformInternal(inter);
            }
        }
        //

        #region  đánh giá thông tin nhập vào của Internal Load
        private void txtNumberOfPeople_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtNumberOfPeople);
        }

        private void txtPeopleLatentHeat_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtPeopleLatentHeat);
        }

        private void txtPeopleSensibleHeat_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtPeopleSensibleHeat);
        }

        private void txtEmittedHeatOfequipment_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtEmittedHeatOfequipment);
        }
        #endregion
        #endregion
        //#region construction 
        private void AddConstruction()
        {
            Glass glas = new Glass();          
            //   luu doi tuong Ceiling 
            Ceiling ceil = new Ceiling();           
            //  luu doi tuong room
            Roof roof = new Roof();        
            //  luu doi tuong wall
            Wall wall = new Wall();       
            //  luu doi tuong floor
            Floor floor = new Floor();           
            //
            bool checking = true;
  
            if (cmbDes_room.Text != "")
            {
                foreach (Glass gl1 in temp.oglass)
                {
                    if (cbxDesConstr.Text == gl1.sName)
                    {
                        MessageBox.Show("Creating new name, please!");
                        checking = false;
                    }
                }
                if (checking)
                {
                    glas.sName = cmbDes_room.Text;
                    floor.sName = cmbDes_room.Text;
                    roof.sName = cmbDes_room.Text;
                    wall.sName = cmbDes_room.Text;

                    ceil.sName = cmbDes_room.Text;
                }
            }
            else
            {
                
                glas.sName = "Construction " + "-" + (temp.oglass.Count + 1);
                floor.sName = "Construction " + "-" + (temp.ofloor.Count + 1);
                roof.sName = "Construction " + "-" + (temp.oroof.Count + 1);
                wall.sName = "Construction " + "-" + (temp.owall.Count + 1);
                ceil.sName = "Construction " + "-" + (temp.oceiling.Count + 1);
            }
            if (checking)
            {

                temp.owall.Add(wall);
                temp.oroof.Add(roof);
                temp.oceiling.Add(ceil);
                temp.oglass.Add(glas);
                temp.ofloor.Add(floor);

            }
          
            button5.IsEnabled = false;
            btncopy_construction.IsEnabled = false;
        }
  
        //
        private void ApplyConstruction()
        {
            //  
          //  glas.sName = cbxDesConstr.Text.ToString();
            glas.sWindowFrameType = cbxTypeOfFrameGlass.Text.ToString();
            glas.dbArea = Convert.ToDouble(txtAreaOfDoor.Text);
            glas.sTypeofglass = cbxTypeOfGlass.Text.ToString();
            glas.dbGlassCoefficient = Convert.ToDouble(txtCoefficientOfGlass.Text);
            glas.sLatitute = cbxLatitute.Text.ToString();
            glas.iTimeofsun = Convert.ToInt32(cbxSolarHour.Text);
            glas.iMonth = Convert.ToInt32(cbxmonth.Text);
            glas.dbPressure = Convert.ToDouble(cbxPressure.Text);
            glas.iTime = Convert.ToInt32(cbxTime.Text);
            glas.dbSolarRadition = Convert.ToDouble(cbxHeatBySolarRadiation.Text);
            glas.sDirection = cbxDirection.Text.ToString();
            glas.dbImmediateCoefficient = Convert.ToDouble(txtImmediate.Text);
            //
            ceil.sName = cbxDesConstr.Text.ToString();
            ceil.dbLength = Convert.ToDouble(txtHeighOfCeilling.Text);
            ceil.dbWidth = Convert.ToDouble(txtWidthOfCeilling.Text);
            ceil.sCharacteristics = cbxCharacteristicsOfCeilling.Text.ToString();
            ceil.sStructure = cbxStructureOfCeilling.Text.ToString();
            ceil.dbHeatTransferCoefficient = Convert.ToDouble(txtHeatTransfer.Text);
            //
            roof.sName = cbxDesConstr.Text.ToString();
            roof.sStructure = cbxStructureOfRoof.Text.ToString();
            roof.dbHeatTransferCoefficient = Convert.ToDouble(txtAbsorptionCoefficient.Text);
            roof.dbLength = Convert.ToDouble(txtLengthOfRoof.Text);
            roof.sCharacteristics = cbxCharacteristicsOfRoof.Text.ToString();
            roof.Surface = cbxTypeOfSurface.Text.ToString();
            //
            floor.sName = cbxDesConstr.Text.ToString();
            floor.dbWidth = Convert.ToDouble(txtWidthOfFloor.Text);
            floor.dbLength = Convert.ToDouble(txtLengthOfFloor.Text);
            floor.sCharacteristics = cbxCharacteristicsOfFloor.Text.ToString();
            floor.sStructure = cbxStructureOfFloor.Text.ToString();
            floor.dbHeatTransferCoefficient = Convert.ToDouble(txtHeatTransferOfFloor.Text);
            //
            wall.sName = cbxDesConstr.Text.ToString();
            wall.dbWidth = Convert.ToDouble(txtWidthOfWall.Text);
            wall.dbLength = Convert.ToDouble(txtLengthOfWall.Text);
            wall.sTypeExposure = cbxTypeOfExposure.Text.ToString();

        }
        //  tao lai cac items trong hop mo ta 
        private void ReloadcomboxConstruct()
        {
            cbxDesConstr.ItemsSource = null;
            cbxDesConstr.Items.Clear();
            cbxDesConstr.ItemsSource = temp.oglass;
            cbxDesConstr.DisplayMemberPath = "sName";
            cbxDesConstr.SelectedIndex = 0;
        }
        //  Load noi dung trang sau khi xoa   
        private void loadSpacing()
        {
            cbxDesConstr.Text = "";
            txtAreaOfDoor.Text = "";
            txtCoefficientOfGlass.Text = "";
            txtCoefficientOfGlass.Text = "";
            txtImmediate.Text = "";
            //
            txtHeighOfCeilling.Text = "";
            txtHeatTransfer.Text = "";
            //
            txtWidthOfWall.Text = "";
            txtLengthOfWall.Text = "";
            //
            txtWidthOfFloor.Text = "";
            txtLengthOfFloor.Text = "";
            txtHeatTransferOfFloor.Text = "";
        }
       

        private void CopyConstruction(Glass g)
        {
             
            try
            {
                //  Copy glass
                Glass copyglass = Activator.CreateInstance<Glass>();
                string name = cbxDesConstr.Text.ToString().Trim();
                Glass oglass = temp.oglass.SingleOrDefault(r => r.sName.Trim().Equals(name));
                int ii = 0;
                foreach (PropertyInfo i in oglass.GetType().GetProperties())
                {
                    ii++;
                    object value = oglass.GetType().GetProperty(i.Name).GetValue(oglass, null);
                    i.SetValue(copyglass, value, null);
                }
                copyglass.sName = "cp" + cbxDesConstr.Text ;
                temp.oglass.Add(copyglass);
                //  Copy ceiling 
                Ceiling copyCeiling = Activator.CreateInstance<Ceiling>();
                Ceiling oceiling = temp.oceiling.SingleOrDefault(rk=>rk.sName.Trim().Equals(name));
                foreach (PropertyInfo i in oceiling.GetType().GetProperties())
                {
                    object value = oceiling.GetType().GetProperty(i.Name).GetValue(oceiling, null);
                    i.SetValue(copyCeiling, value, null);

                }
                copyCeiling.sName = "cpceiling" ;
                temp.oceiling.Add(copyCeiling);
                //  copy floor co van de ----------------------------->
                Floor copyFloor = Activator.CreateInstance<Floor>();
                Floor ofloor = temp.ofloor.SingleOrDefault(rf=>rf.sName.Trim().Equals(name));/// ???
                foreach (PropertyInfo i in ofloor.GetType().GetProperties())
                {
                    object value = ofloor.GetType().GetProperty(i.Name).GetValue(ofloor, null);
                    i.SetValue(copyFloor, value, null);

                }
                copyFloor.sName = "cpfloor" ;
                temp.ofloor.Add(copyFloor);
                //  copy Wall
                Wall copyWall = Activator.CreateInstance<Wall>();
                Wall owall = temp.owall.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                foreach (PropertyInfo i in owall.GetType().GetProperties())
                {
                    object value = owall.GetType().GetProperty(i.Name).GetValue(owall, null);
                    i.SetValue(copyWall, value, null);
                }
                copyWall.sName = "cpwall" ;
                temp.owall.Add(copyWall);
                //  coppy Roof
                Roof copyRoof = Activator.CreateInstance<Roof>();
                Roof oroof = temp.oroof.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                foreach (PropertyInfo i in oroof.GetType().GetProperties())
                {
                    object value = oroof.GetType().GetProperty(i.Name).GetValue(oroof, null);
                    i.SetValue(copyRoof, value, null);
                }
                copyRoof.sName = "cproof";
                temp.oroof.Add(copyRoof);
                //  coppy 
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //  xoa doi tuong construction  
        private void deleteConstruction()
        {
            try
            {
                string name = cbxDesConstr.Text.ToString().Trim();
                Roof oroof = temp.oroof.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                Wall owall = temp.owall.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                Floor ofloor = temp.ofloor.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                Ceiling oceiling = temp.oceiling.SingleOrDefault(rk => rk.sName.Trim().Equals(name));
                Glass oglass = temp.oglass.SingleOrDefault(r => r.sName.Trim().Equals(name));
                temp.oglass.Remove(oglass);
                temp.oceiling.Remove(oceiling);
                temp.ofloor.Remove(ofloor);
                temp.owall.Remove(owall);
                temp.oroof.Remove(oroof);
            }catch(Exception ex1)
            {
                MessageBox.Show("Empty!");
            }
        }
        //  chon doi tuong trong construction 
        private void ChooseObject()
        {
            try
            {
                string name = cbxDesConstr.Text.ToString().Trim();
                Roof oroof = temp.oroof.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                Wall owall = temp.owall.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                Floor ofloor = temp.ofloor.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                Ceiling oceiling = temp.oceiling.SingleOrDefault(rk => rk.sName.Trim().Equals(name));
                Glass oglass = temp.oglass.SingleOrDefault(r => r.sName.Trim().Equals(name));
                //
                cbxDesConstr.Text = oglass.sName;
                cbxTypeOfFrameGlass.Text = oglass.sWindowFrameType.ToString();
                txtAreaOfDoor.Text = oglass.dbArea.ToString();
                cbxTypeOfGlass.Text= oglass.sTypeofglass.ToString();
                txtCoefficientOfGlass.Text=oglass.dbGlassCoefficient.ToString();
                cbxLatitute.Text=oglass.sLatitute.ToString();
                cbxSolarHour.Text = oglass.iTimeofsun.ToString();
                cbxmonth.Text = oglass.iMonth.ToString();
                cbxPressure.Text = oglass.dbPressure.ToString();
                cbxTime.Text= oglass.iTime.ToString();
                cbxHeatBySolarRadiation.Text=oglass.dbSolarRadition.ToString();
                cbxDirection.Text=oglass.sDirection.ToString();
                txtImmediate.Text=oglass.dbImmediateCoefficient.ToString();
              

                //   hien doi tuong Ceiling 


                txtHeighOfCeilling.Text=oceiling.dbLength.ToString();
                txtWidthOfCeilling.Text=oceiling.dbWidth.ToString();
                cbxCharacteristicsOfCeilling.Text=oceiling.sCharacteristics.ToString();
                cbxStructureOfCeilling.Text=oceiling.sStructure.ToString();
                txtHeatTransfer.Text = oceiling.dbHeatTransferCoefficient.ToString();
   

                //  hien doi tuong room

              
                cbxStructureOfRoof.Text=oroof.sStructure.ToString();
                txtAbsorptionCoefficient.Text = oroof.dbHeatTransferCoefficient.ToString();
                txtLengthOfRoof.Text = oroof.dbLength.ToString();
                cbxCharacteristicsOfRoof.Text=oroof.sCharacteristics.ToString();
                cbxTypeOfSurface.Text= oroof.Surface.ToString();
             
                //  hien doi tuong wall


                txtWidthOfWall.Text = owall.dbWidth.ToString();
                txtLengthOfWall.Text=owall.dbLength.ToString();
                cbxTypeOfExposure.Text=owall.sTypeExposure.ToString();
             
                //  hien doi tuong floor
                txtWidthOfFloor.Text = ofloor.dbWidth.ToString();
                txtLengthOfFloor.Text = ofloor.dbLength.ToString();
                cbxCharacteristicsOfFloor.Text= ofloor.sCharacteristics.ToString();
                cbxStructureOfFloor.Text=ofloor.sStructure.ToString();
                txtHeatTransferOfFloor.Text = ofloor.dbHeatTransferCoefficient.ToString();
     
            }
            catch (Exception ex1)
            {
                MessageBox.Show("^@@^");
            }
            
        }
        //
        private void cbxDesConstr_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string name = cbxDesConstr.Text.ToString().Trim();
            glas = (Glass)cbxDesConstr.SelectedItem;
            roof = temp.oroof.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
          wall = temp.owall.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
         floor = temp.ofloor.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
         ceil = temp.oceiling.SingleOrDefault(rk => rk.sName.Trim().Equals(name));
         glas = temp.oglass.SingleOrDefault(r => r.sName.Trim().Equals(name));
            try
            {
                ChooseObject();
            }catch(Exception ex)
            {
                MessageBox.Show("$$$$$");
            }
        }
        //  --sua lai ten button cho phu hop
        //  apply names 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (temp.oroom.Count != 0 && (Room)cmbDes_room.SelectedItem != null)
            {
                ApplyConstruction();
                button5.IsEnabled = true;
                btncopy_construction.IsEnabled = true;
                ReloadcomboxConstruct();
            }
            else
            {
                MessageBox.Show("Input Paramener, please!");
            }
          
        }
        //  button copy/ 
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            if (temp.oglass == null)
            {
                MessageBox.Show("Can't copy!");
            }
            else
            {
                CopyConstruction((Glass)cbxDesConstr.SelectedItem);
                ReloadcomboxConstruct();
            }
        }
        //  button delete 
        private void button5_Click(object sender, RoutedEventArgs e)
        {
            if (temp.oglass != null)
            {
                deleteConstruction();
                ReloadcomboxConstruct();
            }

        }

        private void btn_new_construction_Click(object sender, RoutedEventArgs e)
        {
            AddConstruction();
            ReloadcomboxConstruct();
            button5.IsEnabled = true;
            btncopy_construction.IsEnabled = true;
            btnApplyConstruction.IsEnabled = true;
           
        }
        #region DEFAULT 0
        private void txtAreaOfDoor_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtAreaOfDoor);
        }

        private void txtCoefficientOfGlass_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtCoefficientOfGlass);
        }

        private void txtImmediate_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtImmediate);
        }

        private void txtHeatTransfer_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthOfRoof);
        }

        private void txtHeatTransferOfFloor_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthOfRoof);
        }

        private void txtLengthOfFloor_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthOfRoof);
        }

        private void txtWidthOfFloor_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthOfRoof);
        }

        private void txtLengthOfWall_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthOfRoof);
        }

        private void txtWidthOfWall_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthOfRoof);
        }

        private void txtAbsorptionCoefficient_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthOfRoof);
        }

        private void txtHeatTransferOfRoof_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthOfRoof);
        }

        private void txtLengthOfRoof_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthOfRoof);
        }

        private void txtWidthOfRoof_TextChanged(object sender, TextChangedEventArgs e)
        {
            validationInput(txtWidthOfRoof);
        }
#endregion 

        //
        
    }
}




